package com.jozen.modelbase.netwoek.base;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;

import com.jozen.modelbase.netwoek.eventbus.EventBean;
import com.jozen.modelbase.netwoek.util.AppUtil;
import com.trello.rxlifecycle2.LifecycleTransformer;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Timer;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseActivity<P extends BaseContract.BasePresenter> extends RxAppCompatActivity
        implements BaseContract.BaseView {

    protected Activity mContext;
    private Unbinder mUnBinder;
    protected P mPresenter;
    protected Timer timer;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置状态栏文字颜色及图标为深色
        getWindow().getDecorView().setBackground(null);

        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(getActivityLayoutID());
        //强制横屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        mPresenter = initPresenter();
        attachView();
        mContext = this;
        //初始化butterknife
        mUnBinder = ButterKnife.bind(this);

        ModuleBaseApplication.getInstance().addActivity(this);

        //锁屏管理
        timer = new Timer();

        initView();
        EventBus.getDefault().register(this);

    }

    //界面全屏显示
    private void setNavigationBar() {
        int mUIFlag =
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LOW_PROFILE
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;

        getWindow().getDecorView().setSystemUiVisibility(mUIFlag);
    }


    @Override
    protected void onResume() {
        super.onResume();
        setNavigationBar();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        //记录触摸屏幕的时间
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                View view = getCurrentFocus();
                AppUtil.hideKeyboard(ev, view, BaseActivity.this);//调用方法判断是否需要隐藏键盘
                break;

            default:
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mUnBinder != Unbinder.EMPTY) {
            mUnBinder.unbind();
        }
        if (null != timer) {
            timer.cancel();
            timer = null;
        }

        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }

        getWindow().getDecorView().setBackground(null);//背景制空
        detachView();
        ModuleBaseApplication.getInstance().removeActivity(this);
    }

    @Override
    public void finish() {
        super.finish();
    }

    /**
     * 初始化View
     */
    protected abstract void initView();

    /**
     * 设置Activity的布局ID
     *
     * @return
     */
    protected abstract int getActivityLayoutID();

    /**
     * 在子View中初始化Presenter
     *
     * @return
     */
    protected abstract P initPresenter();


    @Override
    public void showNoNet() {
    }

    @Override
    public void onRetry() {

    }

    @Override
    public <T> LifecycleTransformer<T> bindToLife() {
        return this.bindToLifecycle();
    }

    /**
     * 挂载view
     */
    private void attachView() {
        if (mPresenter != null) {
            mPresenter.attachView(this);
        }
    }

    /**
     * 卸载view
     */
    private void detachView() {
        if (mPresenter != null) {
            mPresenter.detachView();
        }
    }


    /**
     * EventBus接收器
     *
     * @param action
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(EventBean action) {

    }
}
